/**
 * Copyright (c) 2010 icer He <icerhe@gmail.com>
 */
package me.linkcn.ydtxl.client.activity;

import me.linkcn.ydtxl.client.Consts;
import me.linkcn.ydtxl.client.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.TextView;

/**
 * @author icer.He
 * 初始化面画
 */
public class InitialActivity extends Activity {
	
	private static final int DELAY = 4000; //LOGO画面显示时间(毫秒)
	private static final int STEP = 800; //LOGO画面显示时间(毫秒)
	private static final int INIT_SETUP = 1;
	private static final int INIT_NET = 2;
	private static final int INIT_PASS = 3;
	
	private Animation endAnimation;	
	private Handler endAnimationHandler;
	private Runnable endAnimationRunnable;
	private Handler statusHandler;
	private Runnable statusRunnable;
	private TextView statusLabel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(Consts.TAG, "InitialActivity on create");
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.initialscreen);
		statusLabel =  (TextView)findViewById(R.id.status_label);
		
		endAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
		endAnimation.setFillAfter(true);
		
		endAnimationHandler = new Handler();
		endAnimationRunnable = new Runnable() {
			@Override
			public void run() {
				findViewById(R.id.initiallayout).startAnimation(endAnimation);
			}
		};
		
		statusHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (INIT_SETUP == msg.what) {
					statusLabel.setText("初始化配置...");
				} else if (INIT_NET == msg.what) {
					statusLabel.setText("初始化网络...");
				} else if (INIT_PASS == msg.what) {
					statusLabel.setText("读取用户信息...");
				}
			}
			
		};
		statusHandler.removeCallbacks(statusRunnable);
		statusHandler.sendMessageDelayed(statusHandler.obtainMessage(INIT_SETUP), 200);
		statusHandler.sendMessageDelayed(statusHandler.obtainMessage(INIT_NET), 1200);
		statusHandler.sendMessageDelayed(statusHandler.obtainMessage(INIT_PASS), 2200);
		
		endAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) { }
			
			@Override
			public void onAnimationRepeat(Animation animation) { }
			
			@Override
			public void onAnimationEnd(Animation animation) {
				MainActivity.launchSelf(InitialActivity.this);
				InitialActivity.this.finish();
			}
		});
		
		endAnimationHandler.removeCallbacks(endAnimationRunnable);
		endAnimationHandler.postDelayed(endAnimationRunnable, DELAY);
	}
	
}
